﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;

namespace UT3Bots.Visualizer
{

	public partial class Page : UserControl
	{
		private Point _mousePoint;

		public Page()
		{
			InitializeComponent();

			this.Loaded += new RoutedEventHandler(Page_Loaded);
		}

		void Page_Loaded(object sender, RoutedEventArgs e)
		{
			this.utMap.MouseMove += new MouseEventHandler(HandleMouseMove);
			akadia.MouseWheelHelper mouse = new akadia.MouseWheelHelper(this.utMap);
			mouse.Moved += new EventHandler<akadia.MouseWheelEventArgs>(HandleMouseWheel);
		}

		void HandleMouseMove(object sender, MouseEventArgs e)
		{
			_mousePoint = e.GetPosition(this.utMap.gridMap);
		}

		void HandleMouseWheel(object sender, akadia.MouseWheelEventArgs e)
		{
			this.utMap.sliderZoom.Value += e.Delta;

			double offsetX = (_mousePoint.X / this.utMap.ActualWidth) * this.utMap.scrMap.ScrollableWidth;
			double offsetY = (_mousePoint.Y / this.utMap.ActualHeight) * this.utMap.scrMap.ScrollableHeight;

			this.utMap.scrMap.ScrollToHorizontalOffset(offsetX);
			this.utMap.scrMap.ScrollToVerticalOffset(offsetY);

			e.Handled = true;
		}

		private void btnConnect_Click(object sender, RoutedEventArgs e)
		{
			Connect();
		}

		private void TextBox_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.Key == Key.Enter)
			{
				Connect();
			}
		}
		
		private void Connect()
		{
			utMap.Initialize(PageRoot, txtConnect.Text);
			gridConnect.Visibility = Visibility.Collapsed;
		}
	}
}
